package barbershop.com.controller.orders;

import barbershop.com.entity.orders.OrderPayment;
import barbershop.com.service.orders.OrderPaymentService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/payment")
public class OrderPaymentController {
    @Autowired
    private OrderPaymentService orderPaymentService;

    // 获取所有内容
    @RequestMapping("/getAll")
    public List<OrderPayment> findAll(){
        return this.orderPaymentService.findAll();
    }

    // 分页获取数据
    @RequestMapping("/getPage")
    public Page<OrderPayment> getPage(@RequestParam(required = false)Integer page,
                                      @RequestParam(required = false)Integer size){
        return this.orderPaymentService.getPage(page, size);
    }

    @RequestMapping("findByDate")
    public List<OrderPayment> findByDate(@RequestParam(required = false)String date){
        return this.orderPaymentService.findByDate(date);
    }

    /** CRUD **/
    @RequestMapping("/add")
    public Object addOrderPayment(@RequestBody OrderPayment record){
        JSONObject object = new JSONObject();
        // 新增payment
        OrderPayment orderPayment = this.orderPaymentService.insert(record);
        // payment和order主键
        Integer payId = orderPayment.getPayId();
        Integer orderId = record.getOrder().getOrderId();
        String message = this.orderPaymentService.updateTransAmount(payId, orderId);
        object.put("message", message);
        return object;
    }

    @RequestMapping("/delete")
    public Boolean deleteOrderPayment(@RequestBody OrderPayment record){
        return this.orderPaymentService.delete(record);
    }

    @RequestMapping("/update")
    public OrderPayment updateOrderPayment(@RequestBody OrderPayment record){ return this.orderPaymentService.update(record); }

    @RequestMapping("/find")
    public OrderPayment findOrderPayment(@RequestBody OrderPayment record){
        return this.orderPaymentService.find(record);
    }
}
